package com.motong.gongdan.util;

import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Auther: FreddieFang
 * @Date: 2021/6/21 10:21
 * @Description:
 */
public class StringUtil {

    /**
     * 是否为null
     *
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    /**
     * 不为null
     *
     * @param str
     * @return
     */
    public static boolean isNotEmpty(String str) {
        return !StringUtil.isEmpty(str);
    }

    /**
     * 是否为空（包括null）
     *
     * @param str
     * @return 空true，不空false
     */
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 不为空
     *
     * @param str
     * @return
     */
    public static boolean isNotBlank(String str) {
        return !StringUtil.isBlank(str);
    }

    /**
     * 字符串去空
     *
     * @param str
     * @return
     */
    public static String trim(String str) {
        return str == null ? null : str.trim();
    }

    /**
     * 字符串去null
     *
     * @param str
     * @return
     */
    public static String trimToNull(String str) {
        String ts = trim(str);
        return isEmpty(ts) ? null : ts;
    }

    /**
     * 比较字符串
     *
     * @param str1
     * @param str2
     * @return
     */
    public static boolean equals(String str1, String str2) {
        return str1 == null ? str2 == null : str1.equals(str2);
    }

    /**
     * 字符串是否包含
     *
     * @param str
     * @param searchStr
     * @return
     */
    public static boolean contains(String str, String searchStr) {
        if (str == null || searchStr == null) {
            return false;
        }
        return str.indexOf(searchStr) >= 0;
    }

    /**
     * 是否是数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        if (str == null) {
            return false;
        }
        int sz = str.length();
        if (sz == 0) {
            return false;
        }
        for (int i = 0; i < sz; i++) {
            if (Character.isDigit(str.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

    public static boolean isRequireLength(String str, int first, int last) {
        if (str == null) {
            return false;
        }

        int sz = str.length();
        if (sz >= first && sz <= last) {
            return true;
        }
        return false;
    }

    public static int countBytes(String str) throws UnsupportedEncodingException {
        return str.getBytes("UTF-8").length;
    }

    public static boolean isDouble(String str) {
        if (isBlank(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
        return pattern.matcher(str).matches();
    }

    public static String getHall(String str) {
        return String.valueOf(str.charAt(2));
    }

    public static String getRoom(String str) {
        return String.valueOf(str.charAt(0));
    }

    public static String[] getaddress(String address) {
        String[] args = address.split("\\|");
        return args;
    }

    /**
     * 1TB=1024GB 1GB=1024MB 1MB=1024KB 1KB=1024B
     *
     * @param size(byte)
     * @return
     */
    public static String getHtmlFileSize(Long size) {
        if (size < 1024) {
            return size + "B";
        }
        size = size / 1024;
        if (size < 1024) {
            return size + "KB";
        }
        size = size / 1024;
        if (size < 1024) {
            return size + "MB";
        }
        size = size / 1024;
        if (size < 1024) {
            return size + "GB";
        }
        size = size / 1024;
        return size + "TB";
    }

    public static String trimNull(String value) {
        if (value == null) {
            value = "";
        }
        return value;
    }

    public static String getFullName(Integer nameshow, String firstName, String lastName) {
        if (1 == nameshow) {
            return StringUtil.isEmpty(lastName) ? firstName : lastName + " " + firstName;
        }
        return StringUtil.isEmpty(lastName) ? firstName : firstName + " " + lastName;
    }

    /**
     * 转义工具 防止字符串里的标签被转义
     *
     * @param content
     * @return
     */
    public static String escape(String content) {
        return content;
    }


    public static String replaceEnter(String content) {
        if (isNotBlank(content)) {
            content = content.replace("\n", "<br/>");
        }
        return content;
    }
    //俩数组不同元素
    public static <T> List<T> compare(T[] t1, T[] t2) {
        List<T> list1 = Arrays.asList(t1); //将t1数组转成list数组
        List<T> list2 = new ArrayList<T>();//用来存放2个数组中不相同的元素
        for (T t : t2) {
            if (!list1.contains(t)) {
                list2.add(t);
            }
        }
        return list2;
    }

    //俩数组相同元素
    public static Set<String> getIds(String[] a, String[] b){

        Set<String> same = new HashSet<String>();  //用来存放两个数组中相同的元素
        Set<String> temp = new HashSet<String>();  //用来存放数组a中的元素

        for (int i = 0; i < a.length; i++) {
            temp.add(a[i]);   //把数组a中的元素放到Set中，可以去除重复的元素
        }
        for (int j = 0; j < b.length; j++) {
            //把数组b中的元素添加到temp中
            //如果temp中已存在相同的元素，则temp.add（b[j]）返回false
            if(!temp.add(b[j]))
                same.add(b[j]);
        }
        return same;
    }


    //去除数组中重复的记录
    public static String[] array_unique(String[] a) {
        // array_unique
        List<String> list = new LinkedList<String>();
        for(int i = 0; i < a.length; i++) {
            if(!list.contains(a[i])) {
                list.add(a[i]);
            }
        }
        return (String[])list.toArray(new String[list.size()]);
    }

    //合并俩个数组
    public static String[] testC(String [] str,String[] a) {
        List<String> list = new ArrayList<String>();
        for (int i=0; i<str.length; i++) {
            list.add(str[i]);
        }
        for(String s : a){
            list.add(0, s);
        }
        String[] c = list.toArray(new String[1]);
        return c;
    }
    /**
     * 判断是否是整数或者是小数
     * @param str
     * @return true：是，false不是
     */

    public static boolean validateNumber(String str) {

        if(StringUtils.isBlank(str)) {

            return false;

        }
        // 说明一下的是该正则只能识别4位小数；如果不限制小数位数的话，写成[+-]?[0-9]+(\\.[0-9]+)?就可以了

        return str.matches("[+-]?[0-9]+(\\.[0-9]{1,4})?");

    }

    public static Integer apiDataToNumber(String str){
        if(str!=null){
            String a = "" ;
            for(String s : str.split("/")){
                a=s;
                break;
            }
            return Integer.valueOf(a);
        }
        return 0;
    }

    public static boolean isNon(Object object)
    {
        boolean isnon = false;
        if (object == null) {
            return true;
        }
        if ((object instanceof String))
        {
            String str = (String)object;
            if (str.trim().length() == 0) {
                isnon = true;
            }
        }
        else if ((object instanceof Collection))
        {
            Collection collection = (Collection)object;
            if (collection.size() == 0) {
                isnon = true;
            }
        }
        else if ((object instanceof Map))
        {
            Map map = (Map)object;
            if (map.size() == 0) {
                isnon = true;
            }
        }
        return isnon;
    }

    public static boolean isNotNon(Object object)
    {
        return !isNon(object);
    }

    /**
     * 编号生成
     */
    public static String getCode(String type) {
        String first = "";
        if(type.equals("product")){
            first = "CP";
        }else if(type.equals("workOrder")){
            first = "GD";
        }else if(type.equals("processRoute")){
            first = "GYLX";
        }else if(type.equals("process")){
            first = "GX";
        }else if(type.equals("badProduct")){
            first = "BP";
        }else if(type.equals("dep")){
            first = "BM";
        }
        String sdf = new SimpleDateFormat("yyyyMMddHHMMSS").format(new Date());
        return first+sdf;
    }

    public static String regReplace(String str){
        // '*','/',':','?','[','\\',']'
        String s = str.replaceAll("[\\\\\\*/:?$\\{\\}\\[\\]]", "");
        return s;
    }

    public static boolean mobileYZ(String mobile){
        return mobile.matches("^[1]\\d{10}$");
    }

    private static boolean mapCompar(HashMap<String,Object> hashMap,HashMap<String,Object> hashMap2) {
        boolean isChange = false;
        for (Map.Entry<String, Object> entry1 : hashMap.entrySet()) {
            String m1value = entry1.getValue() == null ? "" : (String) entry1.getValue();
            String m2value = hashMap2.get(entry1.getKey()) == null ? "" : (String) hashMap2.get(entry1.getKey());
            if (!m1value.equals(m2value)) {
                isChange = true;
            }
        }
        return isChange;
    }
    public static void main(String[] args) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("key1", "key1");
        hashMap.put("key2", "key2");
        hashMap.put("key3", "key3");
        HashMap<String, Object> hashMap2 = new HashMap<>();
        hashMap2.put("key1", "key1");
        hashMap2.put("key2", "key3");
        System.out.println(hashMap.size());
    }

}
